/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import Entity.Quyen;
import Util.DatabaseHandle;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author copang
 */
public class QuyenImpl implements QuyenDAO{

    public boolean add(Quyen data) {
        String sql = "{ call sp_QUYEN_add(?,?,?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);

        try {
            db.setOutParameter(1, java.sql.Types.INTEGER);
            db.setParameter(2, data.getTenQuyen());
            db.setParameter(3, data.getDienGiaiQuyen());

            db.executeStorePro();
            data.setMaQuyen(db.getIntOutValue(1));
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(QuyenImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean delete(int MaQuyen) {
        String sql = "{ call sp_QUYEN_delete(?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);

        try {

            db.setParameter(1, MaQuyen);
            db.executeStorePro();
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(QuyenImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean edit(Quyen data) {
        String sql = "{ call sp_QUYEN_edit(?,?,?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);

        try {
            db.setParameter(1, data.getMaQuyen());
            db.setParameter(2, data.getTenQuyen());
            db.setParameter(3, data.getDienGiaiQuyen());

            db.executeStorePro();
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(QuyenImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public ResultSet getAll() {
        String sql = "{call sp_Quyen_getAll()}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        return db.excuteQuery();
    }

}
